package com.zh.tea.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.github.pagehelper.PageInfo;
import com.zh.tea.entity.News;
import com.zh.tea.service.NewsService;
import com.zh.tea.service.impl.NewsServiceImpl;

/**
 * Servlet implementation class NewsServlet
 */
@WebServlet("/NewsServlet")
public class NewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewsServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		NewsService newsService = new NewsServiceImpl();
		String opt = request.getParameter("opt");
		if("showNewsList".equals(opt)) {
			String teano = request.getParameter("teano");
			String pagenum = request.getParameter("pagenum");
			String pagesize = request.getParameter("pagesize");
			if (pagenum == null && pagesize == null) {
				pagenum = "0";
				pagesize = "0";
			}
			PageInfo<News> pageinfo = newsService.getNews(teano, Integer.parseInt(pagenum), Integer.parseInt(pagesize));
//			List<News> newslist = pageinfo.getList();
			session.setAttribute("pageinfo", pageinfo);
//			session.setAttribute("newslist", newslist);
//			out.print("<script>location.href='newsCentre.jsp'</script>");
			request.getRequestDispatcher("newsCentre.jsp").forward(request, response);
		}else if("detailNews".equals(opt)) {
			String nid = request.getParameter("nid");
			News news = newsService.getOneNews(Integer.parseInt(nid));
			session.setAttribute("news", news);
			request.getRequestDispatcher("newsDetail.jsp").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
